var mongoose = require('mongoose');
var assert = require('assert');

var Promotions = require('./models/promotions');

//Connection URL
var url = 'mongodb://localhost:27017/conFusion';
mongoose.connect(url);

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'console error: '))

db.once('open', function() {
    //we're connected!
    console.log('Connected correctly to server');

    //Create a new promotion
    Promotions.create({
        "name": "Weekend Grand Buffet",
        "image": "images/buffet.png",
        "label": "New",
        "price": "19.99",
        "description": "Featuring . . ."
    }, function(err, promotion) {
        if (err) throw err;

        console.log('Promotion created!');
        console.log(promotion);

        var id = promotion._id;

        console.log('\n\n\n');
        //get all the promotions
        setTimeout(function() {
            Promotions.findByIdAndUpdate(id, {
                    $set: {
                        description: "Updated Featuring...",
                        label: 'Hot',
                        price: '29.99'
                    }
                }, {
                    new: true
                })
                .exec(function(err, promotion) {
                    if (err) throw err;
                    console.log('Updated description and label and price');
                    console.log(promotion);

                    console.log('\n\n\n');

                    db.collection('promotions').drop(function() {
                        db.close();
                    });
                });
        });
    });
})